<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Modules\Device\Models\Facory;
use Encore\Admin\Layout\Content;
use Encore\Admin\Facades\Admin;
use Illuminate\Support\Facades\DB;
use App\Modules\Device\Services\DeviceService;
use App\Modules\Device\Services\FacoryService;
use Illuminate\Http\Request;

use Encore\Admin\Layout\Row;
use Illuminate\Support\Facades\Input;
use Image;


class DeviceController extends Controller
{
    /**
     * @param Request $request
     * @return Content
     * 品牌列表
     */
    public function facoryList(Request $request)
    {
        $param = $request->all();
        $result = FacoryService::getList($param);
        return Admin::content(function (Content $content) use($result){

            $content->body(view("device.facory_list",compact('result')));
        });

    }

    /**
     * 获取品牌
     */
    public function oneData(Request $request){
        $id = $request->input('id',0);
        if ( empty($id) ) return response()->error("PARAM_EMPTY");
        return response()->success(FacoryService::oneData($id,'id'));
    }

    /**
     * @param Request $request
     * @return mixed
     * 添加或跟新品牌
     */
    public function add_facory(Request $request){
        $param = $request->all();
        $data['brand_name'] = $param['brand_name'];
        $data['facory_name'] = $param['facory_name'];
        $data['device_type'] = $param['device_type'];
        $data['img'] = $param['img'];

        if(!empty($param['id'])){//跟新
            $result = FacoryService::upFacory($param['id'],$data);
            if ( empty($result) ) return response()->error("UPDATE_FAILURE");
        }else {//添加

            $result = FacoryService::addFacory($data);
            if ( empty($result) ) return response()->error("ADD_FAILURE");
        }
        return response()->success();
    }


    /**
     * @param Request $request
     * @return mixed
     * 删除品牌
     */
    public function deleteFacory(Request $request){
        $param = $request->all();
        $id = $param['id'];
        $del['status'] = 0;

        if(empty($param['id'])) return  response()->error("PARAM_EMPTY");
        $data = DB::table('device')->where(['facory_id'=>$id,'is_bundled'=>1,'status'=>1])->first();
        if(!empty($data)) return  response()->error("DELETE_FACORY");

        $result = FacoryService::upFacory($id,$del);
        if ( empty($result) ) return response()->error("DELETE_PRODUCT_FAILED");

        return response()->success();

    }


    /**
     * @return Content
     * 设备列表
     */
    public function deviceList(Request $request){
        $param = $request->all();
        $result = DeviceService::getList($param);

        return Admin::content(function (Content $content) use($result){

            $content->body(view("device.device_list",compact('result')));
        });
    }

    /**
     * @param Request $request
     * @return mixed
     * 产品型号
     */
    public function brand_list(){
        $res = FacoryService::band_list();
        return response()->success($res);
        print_r($res);die;
    }

    /**
     * 添加 编辑设备
     */
    public function add_device(Request $request){

        $param = $request->all();
        $data['device_code'] = $param['device_code'];
        $data['facory_id'] = $param['facory_id'];

        if(!empty($param['id'])){//跟新

            $result = DeviceService::upDevice($param['id'],$data);
            if ( empty($result) ) return response()->error("UPDATE_FAILURE");
        }else {//添加

            $result = DeviceService::addDevice($data);
            if ( empty($result) ) return response()->error("ADD_FAILURE");
        }
        return response()->success();
    }

    /**
     * 获取设备
     */
    public function oneDevice(Request $request){
        $id = $request->input('id',0);
        if ( empty($id) ) return response()->error("PARAM_EMPTY");
        return response()->success(DeviceService::oneDevice($id,'id'));
    }

    //删除列表
    public function deleteDevice(Request $request){

        $param = $request->all();
        $id = $param['id'];
        $del['status'] = 0;

        if(empty($param['id'])) return  response()->error("PARAM_EMPTY");

        $result = DeviceService::upDevice($id,$del);
        if ( empty($result) ) return response()->error("DELETE_PRODUCT_FAILED");

        return response()->success();
    }
    

}
